package com.wolf.cx330.view.view

import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
import com.wolf.cx330.R
import com.wolf.cx330.base.dp

/**
 * 自定义MaterialEditText
 */
@SuppressLint("ResourceType")
class MaterialEditText(context: Context, attributeSet: AttributeSet) :
    AppCompatEditText(context, attributeSet) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val TEXT_SIZE = 12.dp()
    private val TEXT_MARGIN = 8.dp()
    private val VERTICAL_OFFSET = 20.dp()
    private val HORIZONTAL_OFFSET = 4.dp()

    private var colorAccent: Int = 0
    private var floatTextLabelShown = false
    private var anim: ObjectAnimator

    private var animFraction: Float = 0F
        set(value) {
            field = value
            invalidate()
        }
    private var bgRect: Rect = Rect()
    var enabledFloat: Boolean = false
        set(value) {
            if (value != field) {
                if (value) {
                    setPadding(
                        paddingLeft,
                        (paddingTop + TEXT_SIZE + TEXT_MARGIN).toInt(), paddingRight, paddingBottom
                    )
                } else {
                    setPadding(
                        paddingLeft,
                        (paddingTop - TEXT_SIZE - TEXT_MARGIN).toInt(), paddingRight, paddingBottom
                    )
                }
            }
            field = value
        }

    init {
        //  ...
        background.getPadding(bgRect)
        context.obtainStyledAttributes(
            attributeSet, intArrayOf(
                android.R.attr.colorAccent,
                R.attr.enableFloat
            )
        ).apply {
            colorAccent = getColor(0, Color.WHITE)
            enabledFloat = getBoolean(1,false)
            recycle()
        }
        paint.apply {
            paint.textSize = TEXT_SIZE
            paint.color = colorAccent
        }
        anim = ObjectAnimator.ofFloat(this, "animFraction", 1F)
        //  添加文字输入监控
        addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable) {
                if (enabledFloat) {
                    floatTextLabelShown = if (TextUtils.isEmpty(s)) {
                        if (floatTextLabelShown) {
                            anim.reverse()
                        }
                        false
                    } else {
                        if (!floatTextLabelShown) {
                            anim.start()
                        }
                        true
                    }
                }

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (enabledFloat) {
            paint.alpha = ((animFraction * 0xff).toInt())
            val tranYOffset = TEXT_SIZE * animFraction
            canvas.drawText(
                hint.toString(),
                HORIZONTAL_OFFSET,
                VERTICAL_OFFSET + TEXT_SIZE - tranYOffset,
                paint
            )
        }
    }

}